package com.tjvib.sensor;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;

public abstract class SensorData {
    public float timestamp = 0.0f;
    public long localtime = 0;
    public float[] gyr = new float[3];
    public float[] acc = new float[3];
    public float[] mag = new float[3];
    public float[] quat = new float[4];
    public float[] linAcc = new float[3];

    SensorData() {
    }

    SensorData(SensorData d) {
        timestamp = d.timestamp;
        localtime = d.localtime;

        for (int i = 0; i < 3; i++) gyr[i] = d.gyr[i];
        for (int i = 0; i < 3; i++) acc[i] = d.acc[i];
        for (int i = 0; i < 3; i++) mag[i] = d.mag[i];
        for (int i = 0; i < 4; i++) quat[i] = d.quat[i];
        for (int i = 0; i < 3; i++) linAcc[i] = d.linAcc[i];
    }

    SensorData(JSONObject jsonObject) throws JSONException {
        timestamp = (float) jsonObject.getDouble("timestamp");

        localtime = jsonObject.getLong("localtime");

        JSONArray gyrArray = jsonObject.getJSONArray("gyr");
        for (int i = 0; i < 3; i++) {
            gyr[i] = (float) gyrArray.getDouble(i);
        }

        JSONArray accArray = jsonObject.getJSONArray("acc");
        for (int i = 0; i < 3; i++) {
            acc[i] = (float) accArray.getDouble(i);
        }

        JSONArray magArray = jsonObject.getJSONArray("mag");
        for (int i = 0; i < 3; i++) {
            mag[i] = (float) magArray.getDouble(i);
        }

        JSONArray quatArray = jsonObject.getJSONArray("quat");
        for (int i = 0; i < 4; i++) {
            quat[i] = (float) quatArray.getDouble(i);
        }

        JSONArray linAccArray = jsonObject.getJSONArray("linAcc");
        for (int i = 0; i < 3; i++) {
            linAcc[i] = (float) linAccArray.getDouble(i);
        }
    }

    void reset() {
        timestamp = 0;
        localtime = 0;

        for (int i = 0; i < 3; i++) gyr[i] = 0;
        for (int i = 0; i < 3; i++) acc[i] = 0;
        for (int i = 0; i < 3; i++) mag[i] = 0;
        for (int i = 0; i < 4; i++) quat[i] = 0;
        for (int i = 0; i < 3; i++) linAcc[i] = 0;
    }

    public abstract JSONObject toJsonObject() throws JSONException;
}
